package com.ibeetl.admin.console.web;

import com.ibeetl.admin.console.service.OrgConsoleService;
import com.ibeetl.admin.core.annotation.Function;
import com.ibeetl.admin.core.entity.CoreSearch;
import com.ibeetl.admin.core.entity.FiDataCheckDetail;
import com.ibeetl.admin.core.service.CorePlatformService;
import com.ibeetl.admin.core.service.FiDataCheckDetailService;
import com.ibeetl.admin.core.service.FiDataTransDetailService;
import com.ibeetl.admin.core.util.ValidateConfig;
import net.minidev.json.JSONObject;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

@Controller
public class GetManagecom {
    private final Log log = LogFactory.getLog(this.getClass());
    private static final String MODEL = "/admin/GetManageCom";
    @Autowired
    CorePlatformService platformService;

    @Autowired
    private OrgConsoleService orgConsoleService;

    @Autowired
    FiDataTransDetailService fiDataTransDetailService;

    @Autowired
    FiDataCheckDetailService fiDataCheckDetailService;
    @PostMapping(MODEL + "/getManageCom.json")
    @Function("getm")
    @ResponseBody
    public JSONObject getManageCom() {
        Long currentOrgId = this.platformService.getCurrentOrgId();
        List<Long> configContent = new ArrayList<>();
        configContent =orgConsoleService.getAllChildIdsByOrgId(currentOrgId);
        JSONObject ServerJson = new JSONObject();
        for (Long c : configContent) {
            String name = orgConsoleService.queryById(c).getName();
            String code =orgConsoleService.queryById(c).getCode();
            if(code.length()<=6){
                ServerJson.put(code+"", code+""+name);
            }
        }
        log.info("request:selectServer, response:" + ServerJson);
        return ServerJson;
    }

    @PostMapping(MODEL + "/getAllManageCom.json")
    @Function("getm")
    @ResponseBody
    public JSONObject getAllManageCom() {
//        Long currentOrgId = this.platformService.getCurrentOrgId();
        List<Long> configContent = new ArrayList<>();
        long l = Long.parseLong("1");
        configContent =orgConsoleService.getAllChildIdsByOrgId(l);
        JSONObject ServerJson = new JSONObject();
        for (Long c : configContent) {
            String name = orgConsoleService.queryById(c).getName();
            String code =orgConsoleService.queryById(c).getCode();
            ServerJson.put(c+"", code+""+name);
        }
        log.info("request:selectServer, response:" + ServerJson);
        return ServerJson;
    }
    @PostMapping(MODEL + "/isRe.json")
    @Function("getm")
    @ResponseBody
    public String isRe(@Validated(ValidateConfig.ADD.class) CoreSearch search) {
//        Long currentOrgId = this.platformService.getCurrentOrgId();
        String startDate = fiDataTransDetailService.getDate(search.getDateRange(), "0").toString();
        String endDate = fiDataTransDetailService.getDate(search.getDateRange(), "1").toString();
        search.setStartDate(startDate);
        search.setEndDate(endDate);
        List<FiDataCheckDetail> fiDataCheckDetailDatas = fiDataCheckDetailService.isRE(search);
        //	List<FiDataCheckDetail> coreGlInterfacesOfMoney = fiDataCheckDetailService.getCoreGlInterfaceDatasOfMoney(coreSearch);
        //System.out.println(fiDataCheckDetailDatas.size());
        if (fiDataCheckDetailDatas.size()>0){
            return "1";
        }else {
            return "0";
        }

    }
}
